﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Nature
{
    /// <summary>
    /// Represent an equality comparer implying a reduced accuracy measure
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface ITolerance<T>
    {
        /// <summary>
        /// Compares two given values with the reduced accuracy determined by this instance
        /// </summary>
        /// <param name="value1">First value</param>
        /// <param name="value2">Second value</param>
        /// <returns>true if the current instance is equal to the other parameter; otherwise, false.</returns>
        bool AreSame(T value1, T value2);
    }

    public static partial class Extension
    {
        public static ReadOnlyArray<T> AsReadOnlyArray<T>(this T[] array)
        {
            return new ReadOnlyArray<T>(array);
        }

                               
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        public static Dictionary<string, int> ToIndexLookup(this string[] array, IEqualityComparer<string> comparer)
        {
            return array.ToDictionary(i=> i, i => Array.IndexOf(array, i), comparer);
        }

        public static IEnumerable<T> While<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
        {
            foreach (var item in enumerable)
            {
                if (predicate(item))
                {
                    yield return item;
                }
            }
        }

        public static IEnumerable<T> DoWhile<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
        {
            bool firstItem = true;
            foreach (var item in enumerable)
            {
                if (firstItem || predicate(item))
                {
                    yield return item;
                }
                firstItem = false;
            }
        }
    }
}
